//
//  RealTimeFQReportController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/3.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "RealTimeFQReportController.h"
#import "RealTimeTrainModel.h"
#import "ChartItem.h"
#import "PNChart.h"
#import "MTScatterChart.h"
#import "MTBarChartView.h"



@interface RealTimeFQReportController ()
@property (weak, nonatomic) IBOutlet UILabel *trainTimeLongLabel;
@property (weak, nonatomic) IBOutlet UILabel *faqiuShuLabel;
@property (weak, nonatomic) IBOutlet UIView *barSuperView;

@property (weak, nonatomic) IBOutlet UILabel *StandardLabel;

@property (weak, nonatomic) IBOutlet UILabel *averageSpeedLabel;


@property (nonatomic, strong) NSArray *dotItems;

@property (nonatomic, strong) NSArray *barPresents;

@property (nonatomic, strong) NSArray *barMarkTitleArray;

@property (nonatomic, strong) MTBarChartView *barChartView;



@property (weak, nonatomic) IBOutlet MTScatterChart *scatterchartView;


@end

@implementation RealTimeFQReportController {
    
    NSString *_aversgeSpeed;
    __block NSInteger _StandardNum;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _aversgeSpeed = nil;
    _StandardNum = 0;
    [self disposeData];
    [self disposeBarMarkTitleDatas];
    [self setUpUi];

    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.scatterchartView layoutIfNeeded];
    [self.scatterchartView setDefaultValues];
    
    [self.scatterchartView updateTargetSpeed:self.trainModel.faQiuTargetSpeed withTextString:@"目标球速"];
    
    [self.scatterchartView drawChart];
    
    self.scatterchartView.dotItems = self.dotItems;
    [self.scatterchartView updateScatterDotLayer];
 
     [self addBarChartView];
}

// 添加柱状图
- (void)addBarChartView {
    [self.barSuperView layoutIfNeeded];
    CGFloat superHeight = self.barSuperView.bounds.size.height;
    CGFloat superWidth = self.barSuperView.bounds.size.width;
    
    MTBarChartView *barChartView = [[MTBarChartView alloc] initWithFrame:CGRectMake(0, 0, superWidth, superHeight)];
    
    self.barChartView = barChartView;
    
    [self.barSuperView addSubview:self.barChartView];
    
    self.barChartView.barWidth = 30 * (kYFScreenWidth / 375);
    self.barChartView.barSpace = 19 * (kYFScreenWidth / 375);
    self.barChartView.chartMarginLeft = 0;
    self.barChartView.chartMarginBottom = 23;
    self.barChartView.maxBarYValue = superHeight - kAutoHei(50);
    

    self.barChartView.normalBarColor = @[(__bridge id)[UIColor colorWithHex:0x83f09f].CGColor,(__bridge id)[UIColor colorWithHex:0x25c9ad].CGColor];
    self.barChartView.selectBarOpacity = 1.0f;
    self.barChartView.isShow_yAxis = NO;
    self.barChartView.isShow_xAxis = NO;
    self.barChartView.barUserInteractionEnabled = NO;
    self.barChartView.barRadius = 0;
    self.barChartView.axisLineWidth  = 1.5f;

    self.barChartView.barMarkTitleColor = [UIColor colorWithWhite:1.0 alpha:0.8];
    self.barChartView.xTitleArray = @[NSLocalizedString(@"Ball Speed", nil),@"<100",@"100-110",@"110-120",@"120-130",@"130-140",@">140",@"km/h"];
    self.barChartView.barsPercent = self.barPresents;
    self.barChartView.isShowAllMarkLayer = YES;
    self.barChartView.barMarkTitleArray = self.barMarkTitleArray;
    self.barChartView.normalBarOpacity = 1.0;
    self.barChartView.xAxisTextFont = [UIFont systemFontOfSize:10];
    self.barChartView.xAxisTextColor = [UIColor colorWithHex:0x7a7b80 alpha:0.8];
    [self.barChartView drawChart];
    
}

- (void)setUpUi {
    
    UIFont *largeFont =  SanFranciscoFont(26);
    UIFont *smallFont =  [UIFont systemFontOfSize:11];
    
    NSString *paiUnitString = NSLocalizedString(@"Unit.Shots", nil);
    NSString *minUnitString = NSLocalizedString(@"Unit.Minute", nil);
    
    NSString *timelongString = [NSString stringWithFormat:@"%@ %@",[self.trainModel trainTimeLong],minUnitString];
  
    self.trainTimeLongLabel.attributedText = [NSAttributedString attributedString:timelongString subString:@[[self.trainModel trainTimeLong],minUnitString] colors:@[kOrangeColor,kOrangeColor] fonts:@[largeFont, smallFont]];
    
   
     NSString *subString1 = [NSString stringWithFormat:@"%zd",self.trainModel.chartItemArray.count];
    
     NSString *paiShuString = [NSString stringWithFormat:@"%@ %@", subString1,paiUnitString];
    
    self.faqiuShuLabel.attributedText = [NSAttributedString attributedString:paiShuString subString:@[subString1, paiUnitString] colors:@[kOrangeColor,kOrangeColor] fonts:@[largeFont, smallFont]];
    
    NSString *StandardNumString = [NSString stringWithFormat:@"%zd %@",_StandardNum, paiUnitString];
    
    self.StandardLabel.attributedText = [NSAttributedString attributedString:StandardNumString subString:@[@(_StandardNum).stringValue, paiUnitString] colors:@[kOrangeColor,kOrangeColor] fonts:@[largeFont, smallFont]];
    
    self.averageSpeedLabel.text = _aversgeSpeed;
    
    // 设置散点图参数
    self.scatterchartView.yTitleArray = @[@0, @20, @40, @60, @80, @100, @120, @140, @160, @180, @200];
    
    
    
}

// 数据处理
- (void)disposeData {
    
    // 把球速拉出来，求出总达标拍数
    __weak typeof(self) weakSelf = self;
    
    NSMutableArray *arrM = [NSMutableArray array];
    __block CGFloat totalSpeed = 0;
    
    [self.trainModel.chartItemArray enumerateObjectsUsingBlock:^(ChartItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        

        NSInteger maxSpeed = roundf(obj.forecastSpeed);
        
        totalSpeed += maxSpeed;
        
        [arrM addObject:@(maxSpeed)];

        if (maxSpeed >= weakSelf.trainModel.faQiuTargetSpeed) {
            _StandardNum++;
        }
        
    }];
    
    if (arrM.count >= 1) {
        _aversgeSpeed = [NSString stringWithFormat:@"%0.f",totalSpeed / arrM.count];
    }else {
        _aversgeSpeed = @"0";
    }

    self.dotItems = arrM.copy;
}
// 处理柱状图的数据
- (void)disposeBarMarkTitleDatas {
    
    
    __block NSInteger num1 = 0;// <100
    __block NSInteger num2 = 0;// 100-110
    __block NSInteger num3 = 0;//110-120
    __block NSInteger num4 = 0;//120-130
    __block NSInteger num5 = 0;// 130-140
    __block NSInteger num6 = 0;// >140
    
    
    [self.dotItems enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSInteger speed = obj.floatValue;
        
        if (speed < 100) {
            num1++;
        }else if (speed >= 100 && speed < 110){
            num2++;
        }else if (speed >= 110 && speed < 120){
            num3++;
        }else if (speed >= 120 && speed < 130){
            num4++;
        }else if (speed >= 130 && speed < 140){
            num5++;
        }else if (speed >= 140 ){
            num6++;
        }
        
    }];
    
    self.barMarkTitleArray = @[@(0),@(num1),@(num2),@(num3),@(num4),@(num5),@(num6),@(0)];
    
    
    NSArray *barNumArr = [self.barMarkTitleArray sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]]];
    
    NSInteger maxNum = [barNumArr.lastObject integerValue];
    
    if (maxNum <= 0) {
        self.barPresents = @[@(0),@(0),@(0),@(0),@(0),@(0),@(0),@(0)];
    }else {
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:self.barMarkTitleArray.count];
        [self.barMarkTitleArray enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGFloat present = obj.floatValue / maxNum;
            [arrM addObject:@(present)];
        }];
        self.barPresents = arrM.copy;
    }
    
    
    
}

@end
